import Previewer from "vux/src/components/previewer/index.vue";

import { mapGetters } from "vuex";

import template from "./feedbackTemplate";
const { render, staticRenderFns } = template;

export default {
  render,
  staticRenderFns,
  components: {
    Previewer,
  },
  computed: {
    ...mapGetters(["service"]),
  },
  data() {
    return {
      verifyMobile: function (value) {
        return {
          valid: /^[0-9]{11}$/.test(value),
          msg: "输入的手机号码格式错误！",
        };
      },
      suggestion: "",
      cellphone: "",
      option: "建议",
      options: ["建议", "投诉"],

      files: [0, 0, 0],
      btnLoading: false,

      deleteImgPopup: false,
      deleteImgIndex: null,
    };
  },
  mounted() {},
  methods: {
    deleteImg(e, index) {
      this.deleteImgIndex = index;
      this.el = e.currentTarget;
      this.deleteImgPopup = true;
    },
    onConfirm() {
      this.$refs.filesId[this.deleteImgIndex].value = "";
      this.files[this.deleteImgIndex] = 0;
    },
    imgChange(e, index) {
      var el = e.currentTarget;
      var imgUrl = window.URL.createObjectURL(el.files[0]);
      this.$set(this.files, index, imgUrl);
    },
    _getFeedbackInfo() {
      let vm = this;
      if (vm.suggestion == "") {
        vm.$vux.toast.text("请输入反馈意见", "bottom");
        return;
      }

      if (vm.cellphone != "" && !/^[0-9]{11}$/.test(vm.cellphone)) {
        this.$refs.tel.focus();
        this.$vux.toast.show({
          text: "输入的手机号码格式错误！",
        });
        return false;
      }
      var formId = document.getElementById("form");
      var form = new FormData(formId);
      var files = [];
      form.append("token", localStorage.getItem("token"));
      form.append("content", vm.suggestion);

      form.append("type", vm.option == "建议" ? 1 : 2);
      form.append("cellphone", vm.cellphone);
      vm.btnLoading = true;
      var req = new XMLHttpRequest();
      req.open(
        "post",
        vm.urlRequest + "?m=api&c=message&a=getFeedbackInfo",
        true
      );
      req.send(form);
      req.onload = function (e) {
        vm.btnLoading = false;
        var res = JSON.parse(e.target.responseText);
        if (res.status == 0) {
          if (res.ret_msg && res.ret_msg != "") {
            vm.$vux.toast.text(res.ret_msg, "bottom");
          }
          vm.$router.push("/server");
        } else {
          if (res.ret_msg && res.ret_msg != "") {
            vm.$vux.toast.text(res.ret_msg, "bottom");
          }
        }
      };
    },
  },
  watch: {},
};

// WEBPACK FOOTER //
// ./src/components/server/feedback.vue
